﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using NCC_SOP.app_codes;

namespace NCC_SOP.Admin
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                MultiView1.ActiveViewIndex = 0;
                if (Page.User.Identity.IsAuthenticated)
                {
                    MultiView1.ActiveViewIndex = 1;
                }
            }


        }


        protected void checkForActiveUser(object sender, AuthenticateEventArgs e)
        {
            LdapAuthentication adAutheticate = new LdapAuthentication();
            bool adAuthorized = adAutheticate.IsAuthenticated("nccd1", Login1.UserName, Login1.Password);

            e.Authenticated = true;
            if (!Membership.ValidateUser(Login1.UserName, "password"))
            {
                e.Authenticated = false;
                Login1.FailureText = "User Name or Password is incorrect. Please try again.";
            }
            try
            {
                MembershipUser mu = Membership.GetUser(Login1.UserName);

                if (!mu.IsApproved)
                {
                    e.Authenticated = false;
                    Login1.FailureText = "Your account has been deactivated. Please contact your administrator.";
                }

                if (!adAuthorized)
                {
                    e.Authenticated = false;
                    Login1.FailureText = "Your account has been deactivated. Please contact your administrator.";
                }
            }
            catch (NullReferenceException e1)
            {

            }
        }

        protected void LoggedInEvent(object sender, EventArgs e)
        {
            LdapAuthentication adAutheticate = new LdapAuthentication();
            bool adAuthorized = adAutheticate.IsAuthenticated("nccd1", Login1.UserName, Login1.Password);


            //if ( !(FormsAuthentication.GetRedirectUrl(Login1.UserName, Login1.RememberMeSet).Equals(FormsAuthentication.DefaultUrl))
            if ( !adAuthorized  )
            {
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);
            }

            AuditUtil audit = new AuditUtil();
            audit.auditLog_login(Login1.UserName);
        }

        protected void ReturnToDefault(object sender, EventArgs e)
        {
            Response.Redirect("~/Default.aspx");
        }
    }
}